from django.db.models import Count
from django.http import HttpResponseBadRequest
from django.shortcuts import *
from django.views import *
from home.models import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # 导入django分页插件
import os
# 展示数据
class IndexView(View):
    def get(self,req):
        # 1,获取所有分类的信息
        categories = ArticleCategory.objects.all()
        # 2.接受用户点击的分类id
        cat_id = req.GET.get('cat_id',3)
        # 3,根据分类id进行分类的查询
        try:
            category = ArticleCategory.objects.filter(id=cat_id).first()
        except ArticleCategory.DoesNotExist:
            return HttpResponseBadRequest("此类分类信息不存在")
        # 4，获取分页参数
        page_index = req.GET.get('page_index',1) #页码
        page_size = req.GET.get('page_size',3)# 页容量
        #5 根据分类信息查询该分类下的所有文章数据
        articles = Article.objects.filter(category=category)
        #6 创建分页器
        pages = Paginator(articles,page_size) # 对查询到的数据对象articles进行分页，设置超过指定页容量就分页

        avatars = Avatars.objects.all()
        ava_id = Avatars.objects.filter(id=5)
        try:
            list = pages.page(page_index)  # 获取当前页面的记录
        except PageNotAnInteger:
            list = pages.page(1)  # 如果用户输入的页面不是整数时，显示第1页的内容
        # 2-2 重新查询文章信息，按照浏览两降序排序（热门标签）
        hot_tags = Article.objects.values('tags').order_by('-total_views').distinct()[:5]
        # 2-3 最新文章
        new_arts = Article.objects.order_by('-create_time')[:5]
        # 查询评论最多的两条数据 通过article_id分类查询 进行降序排序 保留前两条
        comments = Comment.objects.all().values('article_id').annotate(artic_id=Count('article_id')).order_by('-artic_id')[:2]
        comm_li = []
        for co in comments:
            # 通过字典拿值条件查询找到博客数据
            comm = Article.objects.filter(id=co['article_id'])
            comm_li.append(comm)
        context = {
            'categories':categories,
            'category':category,
            'hot_tags':hot_tags,
            'new_arts':new_arts,
            'articles':list,
            'cat_id':cat_id,
            'avatars':avatars,
            'ava_id':ava_id,
            'comm_li':comm_li
        }
        return render(req,'index.html',context=context)

# 详情视图
class DetailView(View):
    def get(self, req):
        # 1,获取文章id
        art_id = req.GET.get('art_id')
        # 2,根据文章差id查询文章信息
        try:
            art = Article.objects.get(id=art_id)
        except Article.DoesNotExist:
            return render(req,'404.html')
        # 2-1 ,浏览量的简单做法，只要被查询一次，那么就算一次访问
        art.total_views += 1
        art.save()
        # 2-2 重新查询文章信息，按照浏览两降序排序（热门标签）
        hot_tags = Article.objects.values('tags').order_by('-total_views').distinct()[:9]
        # 2-3 最新文章
        new_arts = Article.objects.order_by('-create_time')[:5]
        # 2-4 获取所有评论信息
        comm = Comment.objects.filter(article=art).order_by('-created_time')
        corrtags = Article.objects.filter(tags=art.tags).order_by('?')[:3]
        # 3 返回页面
        context = {
            'article':art,
            'hot_tags':hot_tags,
            'new_arts':new_arts,
            'comms':comm,
            'corrtags':corrtags,

        }
        return render(req, 'details.html', context=context)
    def post(self,req):
        # 1获取已登录用户信息
        user = req.user
        # 2 判断用户是否登录
        if user and user.is_authenticated:
            # 3 登录用户才可以接受form数据
            # 3-1 接收评论数据
            art_id = req.POST.get('art_id')
            content = req.POST.get('content')
            # 3-2 验证文章是否存在
            try:
                art = Article.objects.get(id=art_id)
            except Article.DoesNotExist:
                return HttpResponseBadRequest('该文章不存在')
            # 3-3 保存平路数据
            Comment.objects.create(content=content,article=art,user=user)
            # 3-4 修改文章的评论数量
            art.comments_count += 1
            art.save()
            # 刷新当前页面
            req_url = req.path + '?art_id=' + art_id
            return redirect(req_url)
        else:
            # 4未登录用户则跳转到登录页面
            return redirect(reverse('users:login'))
